import React, { useState } from 'react';
import NumberField from '../../../../../../components/common/NumberField';
import StatefulCalculationCard from '../../../../../../components/common/StatefulCalculationCard';
import ExportData from '../../../../../../components/common/ExportCalculateData';
import type { ResultItem } from '../../../../../../../shared/types';

// 柱子配筋计算表单类型定义
export interface ColumnReinforcementFormValues { 
    columnLength: number,
    columnWidth: number,
    columnHeight: number,
    axialForce: number,
    bottomMoment: number,
    protectiveLayerThickness: number,
    shearForce: number,
}

// 柱子配筋计算结果类型定义
export interface ColumnReinforcementResult {
    sectionArea: number;
    momentToAxialRatio: number;
    minEccentricity: number;
    shearCoefficient: number;
    totalEccentricity: number;
    effectiveHeight: number;
    heightToLengthRatio: number;
    additionalEccentricity: number;
    eccentricityIndicator: number;
    eccentricityType: boolean;
    adjustedEccentricity: number;
    compressionZoneHeight: number;
    steelStrength: number;
    steelArea: number;
}

// 柱子配筋计算初始值配置
const initialValues: ColumnReinforcementFormValues = {
    columnLength: 500,
    columnWidth: 400,
    columnHeight: 3000,
    axialForce: 1000,
    bottomMoment: 50,
    protectiveLayerThickness: 40,
    shearForce: 11.9,
};

// 柱子配筋计算卡片组件配置
const columnReinforcementConfig = {
  key: 'ColumnReinforcement',
  label: '柱子配筋计算',
  formKey: 'crossing-column-reinforcement-calculation',
  initialValues: initialValues,
  calculationType: 'truss-column-reinforcement',
  resultFormatter: (data: ColumnReinforcementResult) => [
    { key: 'sectionArea', name: '截面面积', value: data.sectionArea, unit: 'mm²' },
    { key: 'momentToAxialRatio', name: '弯矩轴力比', value: data.momentToAxialRatio, unit: 'mm' },
    { key: 'minEccentricity', name: '最小偏心距', value: data.minEccentricity, unit: 'mm' },
    { key: 'shearCoefficient', name: '抗剪系数', value: data.shearCoefficient, unit: '' },
    { key: 'totalEccentricity', name: '总偏心距', value: data.totalEccentricity, unit: 'mm' },
    { key: 'effectiveHeight', name: '有效高度', value: data.effectiveHeight, unit: 'mm' },
    { key: 'heightToLengthRatio', name: '高长比', value: data.heightToLengthRatio, unit: '' },
    { key: 'additionalEccentricity', name: '附加偏心距', value: data.additionalEccentricity, unit: 'mm' },
    { key: 'eccentricityIndicator', name: '偏心距指标', value: data.eccentricityIndicator, unit: '' },
    { key: 'eccentricityType', name: '偏心类型', value: data.eccentricityType ? '偏心' : '非偏心', unit: '' },
    { key: 'adjustedEccentricity', name: '调整偏心距', value: data.adjustedEccentricity, unit: 'mm' },
    { key: 'compressionZoneHeight', name: '受压区高度', value: data.compressionZoneHeight, unit: 'mm' },
    { key: 'steelStrength', name: '钢强度设计值', value: data.steelStrength, unit: 'N/mm²' },
    { key: 'steelArea', name: '钢筋计算面积', value: data.steelArea, unit: 'mm²' },
  ],
  renderFormFields: () => (
    <>
      <NumberField name="columnLength" label="柱长" required min={0} step={1} tooltip="柱子的长度，单位为mm" />
      <NumberField name="columnWidth" label="柱宽" required min={0} step={1} tooltip="柱子的宽度，单位为mm" />
      <NumberField name="columnHeight" label="柱高" required min={0} step={1} tooltip="柱子的高度，单位为mm" />
      <NumberField name="axialForce" label="轴力" required min={0} step={1} tooltip="作用在柱子上的轴向力，单位为N" />
      <NumberField name="bottomMoment" label="底部弯矩" required min={0} step={1} tooltip="柱子底部的弯矩，单位为N*mm" />
      <NumberField name="protectiveLayerThickness" label="保护层厚度" required min={0} step={1} tooltip="混凝土保护层的厚度，单位为mm" />
      <NumberField name="shearForce" label="混凝土抗压强度设计值" required min={0} step={0.1} tooltip="混凝土抗压强度设计值，C25混凝土用11.9，单位为MPa" />
    </>
  )
};

// 柱子配筋计算卡片组件实现
const ColunmReinforcementCard: React.FC = () => {
  const [lastResults, setLastResults] = useState<ResultItem[] | null>(null);
  
  return (
    <StatefulCalculationCard
      title={columnReinforcementConfig.label}
      formKey={columnReinforcementConfig.formKey}
      initialValues={columnReinforcementConfig.initialValues}
      renderFormFields={columnReinforcementConfig.renderFormFields}
      calculationType={columnReinforcementConfig.calculationType}
      resultFormatter={(data) => {
        const results = columnReinforcementConfig.resultFormatter(data);
        setLastResults(results);
        return results;
      }}
      columnCount={1} // 使用3列布局
      tableHeight={500} // 设置表格高度为500px
      exportComponent={(form) => (
        <ExportData
          siteName={columnReinforcementConfig.label}
          inputs={form.getFieldsValue()}
          results={lastResults ? Object.fromEntries(lastResults.map(r => [r.key, r.value])) : {}}
          calculationType={columnReinforcementConfig.calculationType}
        />
      )}
    />
  );
};

export default ColunmReinforcementCard;